package com.yuncheng.check.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;

@Controller
@RequestMapping("/api/system/check")
public class CheckController {

    @RequestMapping("/testredirect")
    public void doRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("list?_t=" + System.currentTimeMillis());
    }

    public String getHost() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            return "error in getHost: " + e.getMessage();
        }
        return address.getHostName() + "," + address.getHostAddress();
    }

    private String filterStr(String data, String ... filter) {
        if (null != filter && StringUtils.hasText(data)) {
            StringBuffer buf = new StringBuffer();
            for (String line: StrUtil.split(data, StrUtil.LF)) {
                for(String f: filter) {
                    if (StrUtil.contains(line, f)) {
                        buf.append(line).append(StrUtil.LF);
                    }
                }
            }
            return buf.toString();
        }
        return data;
    }

    @RequestMapping("/list")
    @ResponseBody
    public String index(HttpServletRequest request, HttpServletResponse response) {
        StringBuffer bodys = new StringBuffer();
        bodys.append("<h3><pre>\n");
        bodys.append("getHost(): " + getHost() + "\n");
        bodys.append("remoteAddr: " + request.getRemoteAddr() + "\n");
        bodys.append("remoteHost: " + request.getRemoteHost() + "\n");
        bodys.append("remotePort: " + request.getRemotePort() + "\n");
        bodys.append("remoteUser: " + request.getRemoteUser() + "\n");
        bodys.append("contextPath: " + request.getContextPath() + "\n");
        bodys.append("getPathInfo(): " + request.getPathInfo() + "\n");
        bodys.append("getPathTranslated(): " + request.getPathTranslated() + "\n");
        bodys.append("getServletPath(): " + request.getServletPath() + "\n");
        bodys.append("getProtocol(): " + request.getProtocol() + "\n");
        bodys.append("getgetServerName(): " + request.getServerName() + "\n");
        bodys.append("getServerPort(): " + request.getServerPort() + "\n");
        HttpSession session = request.getSession();
        bodys.append("sessionId=" + session.getId() + "\n");
        bodys.append("session.getMaxInactiveInterval()=" + session.getMaxInactiveInterval() + "\n");
        bodys.append("now()=" + DateUtil.now() + "\n");
        bodys.append("Heades:\n");
        Enumeration<String> e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String item = e.nextElement();
            bodys.append("  " + item + ": " + request.getHeader(item) + "\n");
        }

        bodys.append("banner.txt:vvvvvvvvvvvvvvvvvvvvvvvvvvvv\n");
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("banner.txt");
        bodys.append(filterStr(IoUtil.read(resourceAsStream).toString(), "Build Time:")).append("\n");
        bodys.append("banner.txt:^^^^^^^^^^^^^^^^^^^^^^^^^\n");

        bodys.append("</pre></h3>\n");
        return bodys.toString();
    }

}
